﻿using System;
using System.Windows.Forms;
using Kay.LazarToDo.Win.AppServices;
using Kay.LazarToDo.Win.Common._Enum;
using Kay.LazarToDo.Win.Models;

namespace Kay.LazarToDo.Win._Controls
{
    /// <summary>
    /// 待办事项详情
    /// </summary>
    public partial class LazarToDoDetails : UserControl
    {
        private readonly LazarToDoAppService _lazarToDoAppService;

        /// <summary>
        /// 待办事项
        /// </summary>
        public string ToDoContext
        {
            get => textBoxTodo.Text;
            set => textBoxTodo.Text = value;
        }

        /// <summary>
        /// 待办事项状态
        /// </summary>
        public bool ToDo
        {
            get => radioButtonToDo.Checked;
            set => radioButtonToDo.Checked = value;
        }

        /// <summary>
        /// 待办事项状态
        /// </summary>
        public bool Doing
        {
            get => radioButtonDoing.Checked;
            set => radioButtonDoing.Checked = value;
        }

        /// <summary>
        /// 待办事项状态
        /// </summary>
        public bool Done
        {
            get => radioButtonDone.Checked;
            set => radioButtonDone.Checked = value;
        }

        /// <summary>
        /// 待办事项状态
        /// </summary>
        public bool Remove
        {
            get => radioButtonRemove.Checked;
            set => radioButtonRemove.Checked = value;
        }

        /// <summary>
        /// 待办事项项目
        /// </summary>
        public string Project
        {
            get => linkLabelProject.Text;
            set => linkLabelProject.Text = value;
        }

        /// <summary>
        /// 待办事项元数据
        /// </summary>
        public TjlLazarToDo LazarToDo { get; set; }

        /// <summary>
        /// 初始化待办事项详情
        /// </summary>
        public LazarToDoDetails()
        {
            InitializeComponent();
            _lazarToDoAppService = new LazarToDoAppService();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            var state = ToDoState.ToDo;
            if (radioButtonToDo.Checked)
            {
                state = ToDoState.ToDo;
            }
            else if (radioButtonDoing.Checked)
            {
                state = ToDoState.Doing;
            }
            else if (radioButtonDone.Checked)
            {
                state = ToDoState.Done;
            }
            else if (radioButtonRemove.Checked)
            {
                state = ToDoState.Remove;
            }
            if (LazarToDo != null)
            {
                if (LazarToDo.State != (int)state)
                {
                    var entity = _lazarToDoAppService.UpdateOrDeleteToDo(LazarToDo.Id, state);
                    if (entity != null)
                    {
                        if (state == ToDoState.Remove)
                        {
                            RemoveComplated?.Invoke(this, EventArgs.Empty);
                        }
                        else
                        {
                            UpdateComplated?.Invoke(this, EventArgs.Empty);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 移除待办事项成功事件
        /// </summary>
        public event EventHandler RemoveComplated;

        /// <summary>
        /// 修改待办事项成功事件
        /// </summary>
        public event EventHandler UpdateComplated;
    }
}
